package in.muton.edushare.processing.objects;

import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.vo.CourseDeptLink;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.UnivDeptLink;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.Users;

public class DeptDataHandler implements Serializable {
	private static final long serialVersionUID = 3709660627873068607L;

	public DeptDataHandler(IModel<Departments> dept) {
		this.dept = dept;
	}

	public IModel<Universities> getOwner() {
		List<UnivDeptLink> uD = dept.getObject().getUnivLinks();
		for (UnivDeptLink u : uD) {
			if (u.getAssociationType() == UnivDeptLink.UNIVERSITY_DEPT_OWNER) {
				return new CayenneModel<Universities>(u.getUniversity());
			}
		}
		return null;
	}

	public CayenneList<Universities> getUnivs() {
		List<UnivDeptLink> uD = dept.getObject().getUnivLinks();
		CayenneList<Universities> cL = new CayenneList<Universities>(null);
		for (UnivDeptLink u : uD) {
			cL.add(u.getUniversity());
		}
		return cL;
	}

	public CayenneList<Users> getPeople() {
		List<UserDeptLink> uD = dept.getObject().getUserLinks();
		List<Users> cL = new ArrayList<Users>();
		for (UserDeptLink u : uD)
			cL.add(u.getUserLink());
		Collections.sort(cL);
		return new CayenneList<Users>(cL);
	}

	public CayenneList<Users> getPeopleByRole(int role) {
		List<UserDeptLink> uD = dept.getObject().getUserLinks();
		List<Users> cL = new ArrayList<Users>();
		for (UserDeptLink u : uD) {
			if (u.getRoleType() == role) {
				cL.add(u.getUserLink());
			}
		}
		Collections.sort(cL);
		return new CayenneList<Users>(cL);
	}

	public CayenneList<Users> getPrimaryAdmins() {
		return getPeopleByRole(UserDeptLink.PRIMARY_ADMIN);
	}

	public CayenneList<Users> getUnivAdmins() {
		return new UnivDataHandler(getOwner()).getAdmins();
	}

	public CayenneList<Users> getSecondaryAdmins() {
		return getPeopleByRole(UserDeptLink.SECONDARY_ADMIN);
	}

	public CayenneList<Users> getTeachers() {
		return getPeopleByRole(UserDeptLink.TEACHER);
	}

	public CayenneList<Users> getStudents() {
		return getPeopleByRole(UserDeptLink.STUDENT);
	}

	public CayenneList<Courses> getCourses() {
		List<CourseDeptLink> cD = dept.getObject().getCourseLinks();
		List<Courses> cL = new ArrayList<Courses>();
		for (CourseDeptLink c : cD) {
			cL.add(c.getCourse());
		}
		Collections.sort(cL);
		return new CayenneList<Courses>(cL);
	}

	public CayenneList<Curricula> getCurr() {
		return new CayenneList<Curricula>(dept.getObject().getCurriculums());
	}

	public String getDeptRole(Users user) {
		if (getUnivAdmins().contains(user)) {
			return "University Administrator";
		}
		List<UserDeptLink> uD = user.getDepartmentLinks();
		for (UserDeptLink u : uD) {
			if (u.getDepartmentLink() == dept.getObject()) {
				if (u.getRoleType() == UserDeptLink.PRIMARY_ADMIN)
					return "Department Administrator";
				else if (u.getRoleType() == UserDeptLink.SECONDARY_ADMIN)
					return "Additional Department Administrator";
				else if (u.getRoleType() == UserDeptLink.TEACHER)
					return "Department Faculty";
				else if (u.getRoleType() == UserDeptLink.STUDENT)
					return "Student";
			}
		}
		return "Visitor";
	}

	private IModel<Departments> dept;
}
